<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2019/3/21
 * Time: 11:24
 */
namespace Modules\ApiV2\Controllers;

use App\Models\Goods;
use App\Models\GoodsSku;
use App\Models\ShopOrder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Modules\Service\Logic\ShopOrderLogic;
use Modules\Token\Facades\Token;

class ShopOrderController extends BaseController{
    //生成订单
    public function createShopOrder(Request $request){
        //接收参数
        $userId = Token::data("id");
        $addressId = $request->input("address_id",false);
        //$orderGoodsList = $request->input("orderGoodsList",false);//["goods_id"=>1,"goods_name"=>xxx,"image"=>xxx,"price"=>100,"goods_num"=>2,"sku_id"=>1,"sku_name"=>"红色","sku_price"=>90]
        $couponId = $request->input("coupon_id")?:0;
        $payMethod = $request->input("pay_type",false);
        $openid = $request->input("openid",'');
        //if (!$orderGoodsList)return self::fail(1003,"缺少必要参数:orderGoodsList");
        if (!$payMethod)return self::fail(1003,"缺少必要参数:pay_method");
        if (!$addressId)return self::fail(1003,"缺少必要参数:address_id");
        //Log::info('create order:' . json_encode($orderGoodsList));
        /*
        if(!array_key_exists("goods_id",$orderGoodsList))return self::fail(1024,"缺少元素:goods_id");
        if(!array_key_exists("goods_name",$orderGoodsList))return self::fail(1024,"缺少元素:goods_name");
        if(!array_key_exists("image",$orderGoodsList))return self::fail(1024,"缺少元素:image");
        if(!array_key_exists("price",$orderGoodsList))return self::fail(1024,"缺少元素:price");
        if(!array_key_exists("goods_num",$orderGoodsList))return self::fail(1024,"缺少元素:goods_num");
        if(!array_key_exists("sku_id",$orderGoodsList))return self::fail(1024,"缺少元素:sku_id");
        if(!array_key_exists("sku_name",$orderGoodsList))return self::fail(1024,"缺少元素:sku_name");
        if(!array_key_exists("sku_price",$orderGoodsList))return self::fail(1024,"缺少元素:sku_price");
        */
        $goods_id = $request->input('goods_id');
        $sku_id = $request->input('sku_id',0);
        $goods_num = $request->input('goods_num');
        $version = $request->header("bundle_id","youhuixia");
        $goods = Goods::query()->where('id',$goods_id)->first();
        if (!empty($sku_id)) {
            $sku = GoodsSku::query()->where('id', $sku_id)->first();
        }else{
            $sku = ["id"=>0,"goods_id"=>$goods_id,"sku_name"=>"标准","sku_price"=>$goods["sell_price"]];
        }
        Log::info($goods_id);
        $orderGoodsList = [
            'goods_id'=>$goods_id,'goods_name'=>$goods['goods_name'],'image'=>$goods['image'],'price'=>$goods['sell_price'],
            'sku_id'=>$sku_id,'sku_name'=>$sku['sku_name'],'sku_price'=>$sku['sku_price'],'goods_num'=>$goods_num
        ];
        $logMsg = ['userId'=>$userId,'addressId'=>$addressId,'orderGoodsList'=>$orderGoodsList,'couponId'=>$couponId,'payMethod'=>$payMethod,'openid'=>$openid,'version'=>$version];
        Log::info(json_encode($logMsg));
        return ShopOrderLogic::orderAdd($userId,$addressId,$orderGoodsList,$couponId,$payMethod,$openid,$version);
    }

    //订单再次支付
    public function shopOrderPay(Request $request){
        $orderId = $request->input("order_id",false);
        $payMethod = $request->input("pay_type",false);
        $openid = $request->input("openid",false);
        $version = $request->input("bundle_id",'jiayouhui');
        if (!$orderId)return self::fail(1003,"缺少必要参数:order_id");
        if (!$payMethod)return self::fail(1003,"缺少必要参数:pay_type");
        if ($payMethod=='weixin' && !$openid)return self::fail(1003,"缺少必要参数:openid");
        //获取用户ID
        $userId = Token::data("id");
        $logMsg = ['userId'=>$userId,'orderId'=>$orderId,'payMethod'=>$payMethod,'openid'=>$openid,'version'=>$version];
        Log::info(json_encode($logMsg));
        return ShopOrderLogic::orderPay($userId,$orderId,$payMethod,$openid,$version);
    }

    //商城订单列表
    public function shopOrderList(Request $request){
        $userId = Token::data("id");
        $page = $request->input("page",1);
        $limit = $request->input("limit",20);
        $status = $request->input("status",'all');
        return ShopOrderLogic::orderList($userId,$status,$page,$limit);
    }

    //商城订单详情
    public function shopOrderDetail(Request $request){
        $order_id = $request->input("order_id");
        if (!$order_id)return self::fail(1003,"缺少必要参数:order_id");
        return ShopOrderLogic::orderDetail($order_id);
    }

    //取消订单
    public function shopOrderClose(Request $request){
        $orderId = $request->input("order_id");
        if (!$orderId)return self::fail(1003,"缺少必要参数:order_id");
        return ShopOrderLogic::orderClose($orderId);
    }

    //确认收货
    public function shopOrderFinish(Request $request){
        $orderId = $request->input("order_id");
        if (!$orderId)return self::fail(1003,"缺少必要参数:order_id");
        return ShopOrderLogic::orderFinish($orderId);
    }

    //删除订单
    public function shopOrderDelete(Request $request){
        $orderId = $request->input("order_id");
        if (!$orderId)return self::fail(1003,"缺少必要参数:order_id");
        return ShopOrderLogic::orderDelete($orderId);
    }
}